
import {uuid} from '@/utils/util'
import {loader} from '@/loader'
import message from '@/message'

class RuntimeManager{
    constructor(){
        this.page=null;
        this.pageId='';
        this.uiInstance={};//ui实例
    }

    firstRender(opts){
        const { pagePath,bridgeId}=opts;
        const options=this.makeVueOptions({
            path:pagePath,
            bridgeId
        });
        const root=document.getElementById('#root');
        this.pageId=bridgeId;
        this.page=new Vue(options).$mount();
        root.appendChild(this.page.$el);
        root.addEventListener('scroll',function(){
            message.send({
                type:'pageScroll',
                body:{
                    scrollTop:root.scrollTop,
                    id:bridgeId
                }
            })
        },false)
    }

    makeVueOptions(opts){
        const { path,bridgeId}=opts;
        const staticModule=loader.getModuleByPath(path);
        const self=this;

        return {
            data(){
                return {
                    ...staticModule.data
                }
            },
            created(){
                self.uiInstance[self.pageId]=this;
                message.send({
                    type:'moduleCreated',
                    body:{
                        path,
                        id:self.pageId
                    }
                })
            },
            mounted(){
                self.uiInstance[self.pageId]=this;
                message.send({
                    type:'moduleMounted',
                    body:{
                        id:self.pageId
                    }
                })
            },
            render:staticModule.moduleInfo.render,
        }
    }

}

export default new RuntimeManager();
